package com.kuang.controller;

import com.kuang.pojo.Books;
import com.kuang.service.BookService;
import org.springframework.beans.factory.NamedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    //controller层掉Service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;


    //查询所有的书籍，并且返回到书籍展示页面
    @RequestMapping("/allBooks")
    public String list(Model model){
        List<Books> books = bookService.queryAllBook();
        model.addAttribute("list",books);
        return "allBook";
    }

    //跳转到增加书籍页面
    @RequestMapping("/toAddBooks")
    public String toAddPage(){
        return "addBook";
    }

    //添加书籍得请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBook="+books);
        bookService.addBook(books);

        return "redirect:/book/allBooks";
    }

    //跳转到书籍修改界面
    @RequestMapping("/toUpdateBook")
    public String toUpdatePage(int id,Model model){
        Books books = bookService.queryBookById(id);
        System.out.println("toUpdateBook"+books);
        model.addAttribute("book",books);
        return "updateBook";
    }

    //修改书籍
    @RequestMapping("/updateBook")
    public String updateBook(Books book){
        System.out.println(book);
        bookService.updateBook(book);
        return "redirect:/book/allBooks";
    }

    //删除书籍
    @RequestMapping("/deleteBook")
    public String deleteBook(int id){
        bookService.delBookById(id);
        return "redirect:/book/allBooks";
    }

    //查询书籍
    @RequestMapping("/queryBook")
    public String queryBookByName(String queryBookName,Model model){
        Books books = bookService.queryBookByName(queryBookName);
        List<Books> list = new ArrayList<>();
        list.add(books);
        model.addAttribute("list",list);
        return "allBook";

    }
}
